﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZLDC.Common.Estate;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ZLDC.Topo.BLL.Estate
{
    public class lp_Package
    {

        public static void GetBtnvb(string userid, string depttype, System.Web.UI.HtmlControls.HtmlInputButton btnDetermine, System.Web.UI.HtmlControls.HtmlInputButton btnCantel,Panel sp1,Panel sp2)
        {
            sp2.Visible = false;
            if ("营业部" == depttype || "区域" == depttype)
            {
                btnDetermine.Visible = true;
                btnCantel.Visible = true;
            }
            string sql = "";
            sql = "select COUNT(1) as count,Other4 from lp_Package where CreateUser="+userid+" group by Other4";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Other4"].ToString() == "1")
                {
                    btnDetermine.Value = "套餐已生效";
                }
            }
            else
            {
                
                btnDetermine.Value = "请先新增套餐";
            }
            
        }


        public static void GetTb(DropDownList dropPK, Repeater RpList, string deptlist, string depttype, HiddenField hiduserid)
        {
                        string name = dropPK.SelectedValue;
            if (name != "0")
            {
                string sql = "";
                
                    sql = "select * from lp_Package where   Other3 ='" + name + "' and deptid in(" + deptlist + ") ";
                  
               
                    DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
                    if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
                    {
                        hiduserid.Value = ds.Tables[0].Rows[0]["CreateUser"].ToString();
                    }
                   
                RpList.DataSource = ds.Tables[0];
                RpList.DataBind();

                #region 如果是区域进来 就查找营业部是否有生效的默认套餐
                if (depttype == "区域")
                {
                    string deptSJlist = ZLDC.Topo.BLL.Estate.lp_Basis.GetDeptList(_Utility.GetInfoBySession().m_sDeptID);
                    sql = "select * from lp_Package where   Other4='1' and deptid in(" + deptSJlist + ")";
                    ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
                    if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            foreach (RepeaterItem Item in RpList.Items)
                            {
                                CheckBox ckb = (CheckBox)Item.FindControl("ckb");
                                HiddenField BasisName = (HiddenField)Item.FindControl("hidName");
                                TextBox txtQuantity = (TextBox)Item.FindControl("txtQuantity");
                                HiddenField hidQuantity = (HiddenField)Item.FindControl("hidQuantity");
                                TextBox txtSinglePoint = (TextBox)Item.FindControl("txtSinglePoint");
                                if (ds.Tables[0].Rows[i]["BasisName"].ToString() == BasisName.Value && ds.Tables[0].Rows[i]["Choose"].ToString()=="1")
                                {
                                    if (txtQuantity.Text == "")
                                    {
                                        txtQuantity.Text = ds.Tables[0].Rows[i]["Quantity"].ToString();
                                        hidQuantity.Value = ds.Tables[0].Rows[i]["Quantity"].ToString();
                                        txtSinglePoint.Text = ds.Tables[0].Rows[i]["SinglePoint"].ToString();
                                    }
                                    ckb.Checked = true;
                                    ckb.Enabled = false;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (depttype == "分行")
                {
                    foreach (RepeaterItem Item in RpList.Items)
                    {
                        CheckBox ckb = (CheckBox)Item.FindControl("ckb");
                        HiddenField hidChoosesj = (HiddenField)Item.FindControl("hidChoosesj");
                        if (hidChoosesj.Value == "1")
                        {
                            ckb.Enabled = false;
                        }
                    }

                }
                #endregion
            }
            else
            {
                                RpList.DataSource = null;
                RpList.DataBind();
            }
        }

                                                public static bool Gettr(object obj)
        {

            if (obj.ToString() == "1")
            { return true; }
            else
            {
                return false;
            }
        }

                                                public static bool GetEnabled(object obj)
        {
            if (obj.ToString() == "1")
            { return false; }
            else
            {
                return true;
            }
        }

                                                        public static void GetDrop(DropDownList dropPK, Repeater RpList, string deptlist, string depttype)
        {
            ZLDC.Topo.BLL.Estate.lp_Basis.droplist(dropPK, "type", "namevalue", "select Other3 as namevalue,Name,(Name+CASE WHEN Other2='' then '' else '('+isnull(Other2,'')+')' end)  type from lp_Package  where CreateUser = (" + deptlist + ") Group by Name,Other2,Other3");
                    }

                                        public static string GetDeptList(string deptid)
        {
            string sql = "select top 1* from v_lp_userDeptSuperior where deptid="+deptid;
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
            {
                return ds.Tables[0].Rows[0]["id"].ToString() + "," + ds.Tables[0].Rows[0]["Expr1"].ToString() + "," + ds.Tables[0].Rows[0]["Expr3"].ToString() + "," + ds.Tables[0].Rows[0]["Expr5"].ToString() + ",";
            }
            return "";
        }

                                                public static int GetSJTCcount(string deptid)
        {
            string sql = "";
            sql = "select  top 1* from lp_Package where DeptID in (" + ZLDC.Topo.BLL.Estate.lp_Basis.GetDeptList(deptid) + ") and Other2 in ('区域', '营业部') and Other4='1' order by other2";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
            {
                return 1;
            }
            return 0;
        }

                                                public static int GetSJTCcount1(string deptid)
        {
            string sql = "";
            sql = "select  top 1* from lp_Package where DeptID = (" + deptid + ") and Other2 in ('区域', '营业部')  order by other2";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
            {
                return 1;
            }
            return 0;
        }


        public static void BtnUpdate(DropDownList dropPK, Repeater RpList, Page p, HiddenField hidcount, TextBox txttext, string depttype,HiddenField hiduserid)
        {
            string sql = "";
            string name = dropPK.SelectedValue;
            string namevalue = txttext.Text + DateTime.Now;
            int i = 0;
            Random ran = new Random();
            int RandKey = ran.Next(100);
            foreach (RepeaterItem Item in RpList.Items)
            {
                CheckBox ckb = (CheckBox)Item.FindControl("ckb");
                HiddenField hidid = (HiddenField)Item.FindControl("hidid");
                TextBox txtQuantity = (TextBox)Item.FindControl("txtQuantity");
                TextBox txtScore = (TextBox)Item.FindControl("txtScore");
                HiddenField Remark = (HiddenField)Item.FindControl("hidRemark");
                HiddenField Name = (HiddenField)Item.FindControl("hidName");
                HiddenField Point = (HiddenField)Item.FindControl("hidPoint");
                HiddenField hidCompleteNum = (HiddenField)Item.FindControl("hidCompleteNum");
                HiddenField Unit = (HiddenField)Item.FindControl("hidUnit");
                HiddenField TypeName = (HiddenField)Item.FindControl("TypeName");
                HiddenField hidPackageID = (HiddenField)Item.FindControl("hidPackageID");
                TextBox txtSinglePoint = (TextBox)Item.FindControl("txtSinglePoint");
                int tr = 0;
                int sj = 0;
                i++;
                if (ckb.Checked && ckb.Enabled)
                {
                    tr = 1;
                }
                if (!ckb.Enabled || ckb.Checked)
                {
                    sj = 1;
                }
                if (hidcount.Value == "1")
                {
                    if (hiduserid.Value == _Utility.GetInfoBySession().m_sUserID)
                    {
                        sql += "update lp_Package set UpdateTime='" + DateTime.Now + "',Choosesj="+sj+",SinglePoint=" + (txtSinglePoint.Text == "" ? "0" : txtSinglePoint.Text) + ",UpdateUser=" + _Utility.GetInfoBySession().m_sUserID + ", Score=" + txtScore.Text + ",Quantity=" + txtQuantity.Text + ",Choose=" + tr + " where Other3='" + name + "' and PackageID=" + hidPackageID.Value + " ;";
                    }
                    else
                    {
                        if (txttext.Text == "")
                        {
                           
                            return; 
                        }
                        sql += "insert into lp_Package(Other3,Name,BasisCode,CreateUser,CreateTime,Remark,Quantity,Choose,DeptID,CompleteNum,Score,BasisName,Point,Unit,Other1,Other2,SinglePoint,Choosesj) values (";
                        sql += "'" + namevalue + "','" + (txttext.Text = txttext.Text == dropPK.SelectedValue ? txttext.Text + RandKey.ToString() : txttext.Text) + "','" + hidid.Value + "'," + _Utility.GetInfoBySession().m_sUserID + ",'" + DateTime.Now + "',";
                        sql += "'" + Remark.Value + "'," + (txtQuantity.Text == "" ? "0" : txtQuantity.Text) + "," + tr + "," + _Utility.GetInfoBySession().m_sDeptID + "," + (hidCompleteNum.Value == "" ? "0" : hidCompleteNum.Value) + "," + txtScore.Text + ",'" + Name.Value + "'," + Point.Value + ",'" + Unit.Value + "','" + TypeName.Value + "','" + depttype + "'";
                        sql += "," + (txtSinglePoint.Text == "" ? "0" : txtSinglePoint .Text)+ ","+sj+"";
                        sql += ");";
                    }
                }
                else
                {
                    sql += "update lp_Package set UpdateTime='" + DateTime.Now + "',Choosesj=" + sj + ",SinglePoint=" + (txtSinglePoint.Text == "" ? "0" : txtSinglePoint.Text) + ",UpdateUser=" + _Utility.GetInfoBySession().m_sUserID + ", Score=" + txtScore.Text + ",Quantity=" + txtQuantity.Text + ",Choose=" + tr + " where Other3='" + name + "' and PackageID=" + hidPackageID.Value + " ;";
                }
            }
            if (i > 0)
            {
                using (_Database db = new _Database("estate"))
                {
                    ZLDC.DAL.Estate.lp_Basis.SqlQuery(sql, db);
                }
                p.Response.Write("<script>alert('保存成功！');window.location.href=location.href;</script>");
            }
        }

        public static void BtnAdd(TextBox txtName,Repeater Repeater1,Page p,string depttype)
        {
            string sql = "";
            string name = txtName.Text;
            string namevalue = txtName.Text+DateTime.Now;
            int i = 0;
                                                                                    
           
            foreach (RepeaterItem Item in Repeater1.Items)
            {
                CheckBox ckb = (CheckBox)Item.FindControl("ckb");
                HiddenField Point = (HiddenField)Item.FindControl("hidPoint");
                HiddenField Name = (HiddenField)Item.FindControl("hidName");
                HiddenField Remark = (HiddenField)Item.FindControl("hidRemark");
                HiddenField TypeCode = (HiddenField)Item.FindControl("hidid");
                TextBox txtQuantity = (TextBox)Item.FindControl("txtQuantity");
                HiddenField hidQuantity = (HiddenField)Item.FindControl("hidQuantity");                TextBox txtScore = (TextBox)Item.FindControl("txtScore");
                HiddenField Unit = (HiddenField)Item.FindControl("hidUnit");
                HiddenField TypeName = (HiddenField)Item.FindControl("TypeName");
                TextBox txtSinglePoint = (TextBox)Item.FindControl("TextBox2");
                int tr = 0; 
                i++;
                int sj = 0;                if (ckb.Checked)
                {
                    tr = 1;
                    if (depttype == "营业部" )
                    {
                        sj = 1;
                    }
                    if ( depttype == "区域")
                    {
                        sj = 2;
                    }
                }

                sql += "insert into lp_Package(Other3,Name,BasisCode,CreateUser,CreateTime,Remark,Quantity,Choose,DeptID,CompleteNum,Score,BasisName,Point,Unit,Other1,Other2,SinglePoint,Choosesj) values (";
                    sql += "'"+namevalue+"','" + name + "','" + TypeCode.Value + "'," + _Utility.GetInfoBySession().m_sUserID + ",'" + DateTime.Now + "',";
                    sql += "'" + Remark.Value + "'," + (txtQuantity.Text == "" ? "0" : txtQuantity.Text) + "," + tr + "," + _Utility.GetInfoBySession().m_sDeptID + "," + (hidQuantity.Value == "" ? "0" : hidQuantity.Value) + "," + txtScore.Text + ",'" + Name.Value + "'," + Point.Value + ",'" + Unit.Value + "','" + TypeName.Value + "','"+depttype+"'";
                    sql += "," + (txtSinglePoint.Text == "" ? "0" : txtSinglePoint.Text) + ","+sj+"";    
                sql += ");";
                

            }
            if (i > 0)
            {
                using (_Database db = new _Database("estate"))
                {
                    ZLDC.DAL.Estate.lp_Basis.SqlQuery(sql, db);
                }
                p.Response.Write("<script>alert('保存成功！');window.location.href=location.href;</script>");
            }
        }

        public static void Btn2(DropDownList dropPK,Page p,HiddenField hiduserid)
        {
            if (hiduserid.Value == _Utility.GetInfoBySession().m_sUserID)
            {
                string sql = "delete from lp_Package where Other3='" + dropPK.SelectedValue + "' and CreateUser=" + _Utility.GetInfoBySession().m_sUserID + "";
                p.Response.Write("<script>alert('" + ZLDC.Topo.BLL.Estate.lp_Basis.Execution(sql) + "');window.location.href=location.href</script>");
            }
            else
            {
                p.Response.Write("<script>alert('无法删除上级套餐');window.location.href=location.href</script>");
            }
        }
    }
}
